package com.mingsoft.weixin.action;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import com.mingsoft.base.constant.e.BaseEnum;
import com.mingsoft.basic.entity.AppEntity;
import com.mingsoft.util.MD5Util;
import com.mingsoft.util.StringUtil;
import com.mingsoft.weixin.entity.WeixinEntity;
import com.mingsoft.weixin.util.OauthUtils;

/**
 * 铭飞科技
 * Copyright: Copyright (c) 2014 - 2015 
 * @author 王天培  QQ:78750478
 * Comments:微信模块基础控制
 * Create Date:2015-1-21
 * Modification history:
 */
public abstract class BaseAction extends com.mingsoft.basic.action.BaseAction{
	
	/**
	 * 读取国际化资源文件
	 * 
	 * @param key
	 *            ，键值
	 * @return字符串
	 */
	protected String getResString(String key) {
		return com.mingsoft.weixin.constant.Const.RESOURCES.getString(key);
	}
	
	
	/**
	 * 按照微信的需求生成支付key
	 * @param params 参数
	 * @param key 商户交易key
	 * @return　支付key
	 */
	protected String  getPaySign(Map<String,String> params,String key) {
		//签名步骤一：按字典序排序参数
		Map<String,String> temp = StringUtil.sortMapByKey(params);
		//签名步骤二：在string后加入KEY
		String sign = StringUtil.buildUrl("",temp).replace("?", "")+"&key="+key;
		LOG.debug("=====getPaySign:" + sign);
		//签名步骤三：MD5加密
		sign= MD5Util.MD5Encode(sign,"utf8");
		//签名步骤四：所有字符转为大写
		sign = sign.toUpperCase();
		LOG.debug("====sigin:"+sign);
		return sign;
	}
	
	/**
	 * 设置微信session
	 * @param request HttpServletRequest 对象
	 * @param weixinSession 键SessionConst里面定义(session名称)
	 * @param obj 对象
	 */
	protected void setWeixinSession(HttpServletRequest request,com.mingsoft.weixin.constant.SessionConst weixinSession,Object obj) {
		if (StringUtil.isBlank(obj.toString())) {
			return;
		}
		request.getSession().setAttribute(weixinSession.toString(),obj);
	}
	
	/**
	 * 读取微信session
	 * @param request
	 * @param 微信实体信息
	 */
	protected WeixinEntity getWeixinSession(HttpServletRequest request){
		return (WeixinEntity) request.getSession().getAttribute(com.mingsoft.weixin.constant.SessionConst.WEIXIN_SESSION.toString());
	}
	
	/**
	 * 组装授权地址
	 * @param link授权后跳转地址
	 * @param state扩展参数最长128字节
	 * @param isUserInfo true用户详细信息，false:openid详细见微信接口
	 * @param request当前请求对象
	 * @return 返回组织后的请求地址
	 */
	public String builderOauthUrl(String link,String state,boolean isUserInfo,HttpServletRequest request) {
		AppEntity app = this.getApp(request);
		if (link.toLowerCase().indexOf("http://")<0)  {
			link = app.getAppHostUrl()+"/"+link;
		}
		WeixinEntity weixin = this.getWeixinSession(request);
		String url = OauthUtils.builderOauthUrl(link, state,isUserInfo,weixin.getWeixinAppID());
		LOG.debug("oauthUrl:"+url);
		return url;
	}
	
	/**
	 * 枚举转list
	 * @param <T>
	 * @param cls 实现了BaseEnum的子类
	 * @return 转换失败返回null
	 */
	protected <T> List<Map<String,Object>> weixinEnumToList(Class<T> cls) {
		List<Map<String,Object>> list = null;
		if (cls!=null) {
			list = new ArrayList<Map<String,Object>>();
			try {
				Method method = cls.getDeclaredMethod("values");
				BaseEnum[] be = (BaseEnum[]) method.invoke(cls); 	
				for (BaseEnum e:be) {
					Map<String,Object> map = new HashMap<String,Object>();
					map.put("id", e.toInt());
					map.put("value", e.toString());
					list.add(map);
				}
			} catch(Exception e) {
				return null;
			}

		}
		return list;
	}	
	
}
